package com.jmu.content.controller.admin;


import com.jmu.constant.JwtClaimsConstant;
import com.jmu.content.dto.EmployeeLoginDTO;
import com.jmu.content.pojo.Employee;
import com.jmu.content.result.Result;
import com.jmu.content.service.EmployeeService;
import com.jmu.utils.JwtUtil;
import com.jmu.content.vo.EmployeeLoginVO;
import com.jmu.context.BaseContext;
import com.jmu.content.properties.JwtProperties;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminEmployeeController")
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @PostMapping
    @ApiOperation("员工登出")
    public Result logout(){
        BaseContext.removeCurrentId();
        return Result.success();
    }

}

